import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-01-13
 * Time: 16:07
 */
public class Test {
    //创建一个用于测试的逆序数组
    public static void initArrayOrder(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
//            array[i] = array.length-i;
//            array[i]=i;
            array[i] = random.nextInt(1000);

        }
    }

    //测试插入排序
    public static void testInsertSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.insertSort(array);
        long end = System.currentTimeMillis();
        System.out.println("插入排序："+(end-start));
    }
    //测试希尔排序
    public static void testShellSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.shellSort(array);
        long end = System.currentTimeMillis();
        System.out.println("希尔排序："+(end-start));

    }
    //测试选择排序1
    public static void testSelectSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.selectSort(array);
        long end = System.currentTimeMillis();
        System.out.println("选择排序1："+(end-start));

    }
    //测试选择排序2
    public static void testSelect2Sort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.selectSort2(array);
        long end = System.currentTimeMillis();
        System.out.println("选择排序2："+(end-start));

    }
    //测试冒泡排序
    public static void testBubbleSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.bubbleSort(array);
        long end = System.currentTimeMillis();
        System.out.println("冒泡排序："+(end-start));

    }
    //测试堆排序
    public static void testHeapSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.heapSort(array);
        long end = System.currentTimeMillis();
        System.out.println("堆排序："+(end-start));

    }
    //测试快速排序
    public static void testQuickSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.quickSort(array);
        long end = System.currentTimeMillis();
        System.out.println("快速排序："+(end-start));

    }
    //测试快速排序2->非递归
    public static void testQuickSort2(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.quickSort2(array);
        long end = System.currentTimeMillis();
        System.out.println("快速排序2："+(end-start));

    }
    //测试归并排序
    public static void testMergeSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.mergeSort(array);
        long end = System.currentTimeMillis();
        System.out.println("归并排序："+(end-start));

    }
    //测试归并排序2->非递归
    public static void testMergeSort2(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long start = System.currentTimeMillis();
        Sort.mergeSort2(array);
        long end = System.currentTimeMillis();
        System.out.println("归并排序2："+(end-start));

    }



    public static void main(String[] args) {
        int[] array = new int[10_0000];
        initArrayOrder(array);
//        int[] array = {17,9,99,32,7,21,58,11,64,44};


        testInsertSort(array);

        testShellSort(array);

        testSelectSort(array);

        testSelect2Sort(array);

        testBubbleSort(array);

        testHeapSort(array);

        //再测试的时候，逆序int[] array = new int[100000]会栈溢出，而顺序不会
        //目前我的理解是，逆序情况下需要swap较多，在栈上占用空间较大
        testQuickSort(array);

        testQuickSort2(array);

        testMergeSort(array);

        testMergeSort2(array);

        System.out.println();
    }

}
